﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public partial class TrackingManage_TrackingAdd : BasePage
{
    IBNTrackingHandler trackingManager = new BNTrackingHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1067")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }
            else
            {
                Response.Redirect("../Login.aspx");
            }

            dataBind();
        }
    }
    private void dataBind()
    {
        ISyTruckStatusHandler tsManager = new SyTruckStatusHandler();
        string sqlStr = "SELECT * FROM Sy_TruckStatus WHERE Status = 1 ORDER BY Sequence asc";
        DataTable dt = tsManager.GetList(sqlStr, null);
        string language = Thread.CurrentThread.CurrentUICulture.ToString().ToLower();
        ddlTruckStatus.DataSource = dt;
        ddlTruckStatus.DataTextField = ("en".Equals(language) ? "ENGStatusName" : ("zh-hk".Equals(language) ? "HKStatusName" : "StatusName"));
        ddlTruckStatus.DataValueField = "PKID";
        ddlTruckStatus.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string truckID = Request["TID"].ToString();
        string sendTruckID = Request["STID"].ToString();
        string truckStatusID = ddlTruckStatus.SelectedValue;
        string sqlStr = "SELECT * FROM BN_TTracking WHERE TruckID=@TruckID AND SendTruckID=@SendTruckID AND TruckStatusID=@TruckStatusID";
        SqlParameter[] parms = new SqlParameter[3];
        parms[0] = new SqlParameter("@TruckID", truckID);
        parms[1] = new SqlParameter("@SendTruckID", sendTruckID);
        parms[2] = new SqlParameter("@TruckStatusID", truckStatusID);

        DataTable dt = trackingManager.GetList(sqlStr, parms);

        if (dt != null && dt.Rows.Count > 0)
        {
            Jscript.Alert(Resources.Messages.TruckStatusExists, this.Page);
            return;
        }

        ISyTruckStatusHandler tsManager = new SyTruckStatusHandler();
        Sy_TruckStatusQuery ts = tsManager.GetTruckStatusByID(int.Parse(truckStatusID));
        if (ts != null)
        {
            sqlStr = @"SELECT MAX(Sequence) FROM Sy_TruckStatus WHERE PKID IN (
                    SELECT DISTINCT TruckStatusID FROM BN_TTracking WHERE TruckID=@TruckID AND SendTruckID=@SendTruckID) AND Status = 1";
            parms = new SqlParameter[2];
            parms[0] = new SqlParameter("@TruckID", truckID);
            parms[1] = new SqlParameter("@SendTruckID", sendTruckID);

            dt = trackingManager.GetList(sqlStr, parms);
            if (dt != null && dt.Rows.Count > 0)
            {
                object tmp = dt.Rows[0][0].ToString();
                if (tmp != null && tmp != "")
                {
                    int seq = int.Parse(tmp.ToString());
                    if (ts.Sequence <= seq)
                    {
                        Jscript.Alert(Resources.Messages.WrongTruckStatusSequence, this.Page);
                        return;
                    }

                    sqlStr = "SELECT * FROM Sy_TruckStatus WHERE Sequence > @seq1 AND Sequence < @seq2";
                    parms = new SqlParameter[2];
                    parms[0] = new SqlParameter("@seq1", seq.ToString());
                    parms[1] = new SqlParameter("@seq2", ts.Sequence);

                    dt = trackingManager.GetList(sqlStr, parms);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        Jscript.Alert(Resources.Messages.WrongTruckStatusSequence, this.Page);
                        return;
                    }
                }
            }

            BN_TTrackingQuery query = new BN_TTrackingQuery();
            query.TruckID = int.Parse(truckID);
            query.SendTruckID = int.Parse(sendTruckID);
            query.TruckStatusID = int.Parse(truckStatusID);
            string date = txtCBDate.Text.Substring(0, 10);
            query.CBDate = DateTime.Parse(date + StringUtil.GetTime(txtCBDateH.Text));
            query.CEDate = query.CBDate.AddHours(double.Parse(ts.Interval.ToString()));
            query.CreatedUser = int.Parse(Session["UserID"].ToString());
            query.CreatedDate = DateTime.Now;
            query.Remark = txtRemark.Text;

            if (trackingManager.AddTracking(query))
            {
                Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "TrackingDtl.aspx?STID=" + sendTruckID + "&TID=" + truckID + "&rtn=" + Request["rtn"].ToString(), this.Page);
                return;
            }
            else
            {
                Jscript.Alert(Resources.Messages.AddFailed, this.Page);
            }
        }
        
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("TrackingDtl.aspx?STID=" + Request["STID"].ToString() + "&TID=" + Request["TID"].ToString() + "&rtn=" + Request["rtn"].ToString());
    }
}
